package cn.cvs.service;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service("SysUserServiceImpl")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public SysUser login(String account, String password) throws Exception {

        SysUser sysUser=this.sysUserMapper.selectByAccount(account);
        if (null!=sysUser && !sysUser.getPassword().equals(password))
        {
             sysUser=null;
        }
        return sysUser;
    }


    @Override
    public List<SysUser> getUserList(String realName,String roleId) throws Exception {
        return this.sysUserMapper.selectList(realName,roleId);
    }

    @Override
    public int getUserCount(String realName, Integer roleId) throws Exception {
        return this.sysUserMapper.selectCount(realName, roleId);
    }

    @Override
    public int add(SysUser sysUser) throws Exception {
       return this.sysUserMapper.add(sysUser);
    }


    @Override
    public SysUser getUserById(Integer id) throws Exception {
        return this.sysUserMapper.selectById(id);
    }
    @Override //修改
    public boolean update(SysUser sysUser) throws Exception {
        boolean flag = false;
        sysUser.setUpdatedTime(new Date());
        if (this.sysUserMapper.update(sysUser) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public int deleteById(Integer id) throws Exception {
        return this.sysUserMapper.deleteById(id);
    }
    @Override
    public boolean updatePwd(Integer id, String pwd) throws Exception {
        boolean flag = false;
        if (this.sysUserMapper.updatePwd(id, pwd) > 0) {
            flag = true;
        }
        return flag;
    }
    @Override
    public SysUser getByAccount(String account) throws Exception {
        return this.sysUserMapper.selectByAccount(account);
    }
}
